import { makeAutoObservable } from "mobx";
import Taro from "@tarojs/taro";
import { DynamicColors } from "@/utils/DarkMode";
import AppThemeMode from "../utils/DarkMode/AppThemeMode";

export interface IThemeMode {
  isDarkMode: boolean;
}

class AppState {
  constructor() {
    makeAutoObservable(this);
    AppThemeMode.addListener(this.modeChange);
  }

  themeMode: IThemeMode = { isDarkMode: AppThemeMode.isDarkMode };

  init: boolean = false; // 在App.tsx 完成一系列初始化操作后，标志初始化完成

  modeChange = () => {
    this.updateDarkMode(AppThemeMode.isDarkMode);
  };

  updateDarkMode(isDarkMode: boolean) {
    console.log("updateDarkMode", isDarkMode);
    this.themeMode = { isDarkMode };
    Taro.setNavigationBarColor({
      backgroundColor: DynamicColors.navBarColor.value,
      frontColor: DynamicColors.frontColor.value,
    });
  }

  updateInit(init: boolean) {
    console.log("updateInit", init);
    this.init = init;
  }
}

const AppState_Store_Key = "appState";
export { AppState, AppState_Store_Key };

export default new AppState();
